package handler

import (
	config2 "360/api/basic/config"
	"360/api/pkg"
	req2 "360/api/req"
	"360/video-srv/basic/config"
	__ "360/video-srv/basic/proto"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"math/rand"
	"net/http"
	"path"
	"strconv"
	"time"
)

func Send(ctx *gin.Context) {
	var req req2.SendReq
	ctx.ShouldBind(&req)
	code := rand.Intn(900000) + 100000
	sms, _ := pkg.SendSms(req.Phone, strconv.Itoa(code))
	if *sms.Body.Code != "OK" {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": *sms.Body.Message,
			"data":    nil,
		})
	}
	count, _ := config.Rdb.Get(config.Ctx, req.Phone+"conunt").Int()
	if count >= 3 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "5分钟内已发送超过三条短信,禁止发送",
			"data":    nil,
		})
	} else {
		config.Rdb.Set(config.Ctx, req.Phone, code, time.Minute*5)
		if count == 0 {
			config.Rdb.Set(config.Ctx, req.Phone+"conunt", 1, time.Minute*5)
		} else {
			config.Rdb.Incr(config.Ctx, req.Phone+"conunt")
		}

		ctx.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "发送短信成功",
			"data":    nil,
		})
	}

}
func Login(ctx *gin.Context) {
	var req req2.LoginReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.Login(ctx, &__.LoginReq{
		Phone:    req.Phone,
		Password: req.Password,
		Code:     req.Code,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "登录注册成功",
		"data":    &__.LoginResp{UserId: r.UserId},
	})
}
func Upload(ctx *gin.Context) {
	var req req2.Upload
	ctx.ShouldBind(&req)
	file, m, err := ctx.Request.FormFile("image")
	if err != nil {
		return
	}
	upload, err := pkg.Upload(file, m)
	if err != nil {
		return
	}

	if m.Size >= 1024*1024*1024 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "大小不能大于1GB",
			"data":    nil,
		})
	} else {
		ext := path.Ext(m.Filename)
		if ext != ".mp4" {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    500,
				"message": "格式不正确",
				"data":    nil,
			})
		} else {
			if pkg.BaiDu(upload) {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    200,
					"message": "上传成功",
					"data":    upload,
				})
			} else {
				ctx.JSON(http.StatusOK, gin.H{
					"code":    500,
					"message": "上传失败,审核不通过",
					"data":    nil,
				})
			}

		}
	}
}
func VideoAdd(ctx *gin.Context) {
	var req req2.VideoAddReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.VideoAdd(ctx, &__.VideoAddReq{
		Title:        req.Title,
		Description:  req.Description,
		CoverUrl:     req.CoverUrl,
		Category:     req.Category,
		Types:        req.Types,
		Region:       req.Region,
		Year:         int64(req.Year),
		Actor:        req.Actor,
		PlayCount:    int64(req.PlayCount),
		LikeCount:    int64(req.LikeCount),
		CollectCount: int64(req.CollectCount),
		CommentCount: int64(req.CommentCount),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "添加成功",
		"data": &__.VideoAddResp{
			VideoId: r.VideoId,
		},
	})
}
func VideoList(ctx *gin.Context) {
	var req req2.VideoListReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.VideoList(ctx, &__.VideoListReq{
		Page:     int64(req.Page),
		Size:     int64(req.Size),
		Category: int64(req.Category),
		Types:    int64(req.Types),
		Region:   int64(req.Region),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "展示成功",
		"data": &__.VideoListResp{
			List: r.List,
		},
	})
}
func VideoFind(ctx *gin.Context) {
	var req req2.VideoFindReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.VideoFind(ctx, &__.VideoFindReq{
		VideoId: int64(req.VideoId),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查看成功",
		"data": &__.VideoFindResp{
			Title:        r.Title,
			Description:  r.Description,
			CoverUrl:     r.CoverUrl,
			Category:     r.Category,
			Types:        r.Types,
			Region:       r.Region,
			Year:         r.Year,
			Actor:        r.Actor,
			PlayCount:    r.PlayCount,
			LikeCount:    r.LikeCount,
			CollectCount: r.CollectCount,
			CommentCount: r.CommentCount,
			List:         r.List,
		},
	})
}
func VideoLog(ctx *gin.Context) {
	var req req2.VideoLogReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.VideoLog(ctx, &__.VideoLogReq{
		UserId:   int64(req.UserId),
		VideoId:  int64(req.VideoId),
		EpId:     int64(req.EpId),
		EpTimeAt: strconv.Itoa(req.EpTimeAt),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "观看成功",
		"data": &__.VideoLogResp{
			VideoEpLogId: r.VideoEpLogId,
		},
	})
}
func VideoLogListReq(ctx *gin.Context) {
	var req req2.VideoLogListReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.VideoLogList(ctx, &__.VideoLogListReq{
		UserId: int64(req.UserId),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "浏览历史记录成功",
		"data":    &__.VideoLogListResp{List: r.List},
	})
}
func HotList(ctx *gin.Context) {
	var req req2.HotListReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.HotList(ctx, &__.HotListReq{})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "展示成功",
		"data": &__.HotListResp{
			List: r.List,
		},
	})
}

func EsSync(ctx *gin.Context) {
	var req req2.EsSyncReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.EsSync(ctx, &__.EsSyncReq{
		VideoId: int64(req.VideoId),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	fmt.Println(&r)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "同步成功",
		"data":    &__.EsSyncResp{},
	})
}
func EsSearch(ctx *gin.Context) {
	var req req2.EsSearchReq
	ctx.ShouldBind(&req)
	r, err := config2.Video.EsSearch(ctx, &__.EsSearchReq{
		KeyWord: req.KeyWord,
		Page:    int64(req.Page),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "高光搜索成功",
		"data": &__.EsSearchResp{
			List: r.List,
		},
	})
}
